import baseRequest from '../base'
import { DateModel } from '@/utils/date'

/**
 * 新增模板
 * @param roleName 发布模板名字 String
 * @param couponId 选择礼券id Number
 * @param pushRadio 推送比例 Number
 * @param templateValueList 选择的组合标签id Object item => labelId: ''
 */
export async function getAddTemplate(data) {
  const res = await baseRequest({
    url: 'template/create',
    method: 'post',
    data
  })
  if (res.resultCode === '9001') {
    return res
  }
}

export async function getUpdateTemplate(data) {
  const res = await baseRequest({
    url: 'template/update',
    method: 'post',
    data
  })
  if (res.resultCode === '9001') {
    return res
  }
}

/**
 * 分页查询模板列表
 * @param pageNum
 * @param pageSize
 * @param queryParamMap
 * @param roleName
 * @param pushStatus
 * @param startPushDate
 * @param endPushDate
 * @param createStartDate
 * @param createEndDate
 */
export async function getTemplateList(data) {
  const res = await baseRequest({
    url: 'template/query_template_list',
    method: 'post',
    data
  })
  if (res.resultCode === '9001') {
    const { result } = res
    for (let i = 0; i < result.content.length; i++) {
      result.content[i].createTime = DateModel.timestampToDate(result.content[i].createTime, false).split(' ')[0]
      result.content[i].pushDate = DateModel.timestampToDate(result.content[i].pushDate, false)
      if (result.content[i].statisticsDeadline) {
        result.content[i].statisticsDeadline = DateModel.timestampToDate(result.content[i].statisticsDeadline, false).split(' ')[0]
      }
      if (result.content[i].updateTime) {
        result.content[i].updateTime = DateModel.timestampToDate(result.content[i].updateTime, false).split(' ')[0]
      }
    }
    return result
  }
}

export async function getTemplateDetail(templateId) {
  const res = await baseRequest({
    url: `template/query_detail/${templateId}`,
    method: 'get'
  })
  if (res.resultCode === '9001') {
    res.template.pushDate = DateModel.timestampToDate(res.template.pushDate, false)
    res.template.statisticsDeadline = DateModel.timestampToDate(res.template.statisticsDeadline, false).split(' ')[0]
    return res
  }
}

/**
 * 删除模板
 */
export async function getDeleteTemplate(data) {
  const res = await baseRequest({
    url: `template/delete`,
    method: 'post',
    data
  })
  if (res.resultCode === '9001') {
    return res
  }
}

/**
 * 获取标签对应预计人数
 */
export async function getLabelNum(data) {
  const res = await baseRequest({
    url: `template/query_label_num`,
    method: 'post',
    data
  })
  if (res.resultCode === '9001') {
    return res
  }
}

/**
 * 查询推送进度
 */
export async function getRoleSchedule(roleId) {
  const res = await baseRequest({
    url: `template/selectRoleSchedule/${roleId}`,
    method: 'get'
  })
  if (res.resultCode === '9001') {
    return res
  }
}

/**
 * 查询未推送店长信息
 * @param roleId 当前发放模板的id
 */
export async function getSenderInfo(data) {
  const res = await baseRequest({
    url: `template/query_sender_info`,
    method: 'post',
    data
  })
  console.log('未推送', res)
  if (res.resultCode === '9001') {
    return res
  }
}

/**
 * 发送消息提醒
 * @param userIds Array 店长集合
 */
export async function getSendMessage(data) {
  const res = await baseRequest({
    url: `template/message_send`,
    method: 'post',
    data
  })
  console.log(res)
  if (res.resultCode === '9001') {
    return res
  }
}
